﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace RubyUnitTest
{
    public partial class Form1 : Form
    {
        int i;
        string rubyfile, testfile;

        public Form1()
        {
            InitializeComponent();
            runTestToolStripMenuItem.Enabled = false;
        }

        private void loadTestInputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open test input file";
            fDialog.Filter = "Text Files|*.txt|XML Files|*.xml";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                testfile = fDialog.FileName.ToString();
                LoadFile();
            }
            if(testfile != null && rubyfile != null)
                runTestToolStripMenuItem.Enabled = true;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Ruby Unit Test Tool 0.1\nAuthor: Bo Feng");
        }

        private void LoadFile()
        {
            i = 0;
            StreamReader sr;
            StreamWriter sw;
            if(!System.IO.Directory.Exists("c:\\temp"));
                System.IO.Directory.CreateDirectory("c:\\temp");
            sw = File.CreateText("c:\\temp\\list.html");
            sw.WriteLine("<html><body><table width=\"100%\" style=\"border:1px solid;\"><tr><td style=\"background-color: #CCCCCC\" width=\"50%\">Input</td><td style=\"background-color: #CCCCCC\" width=\"50%\">Expected Output</td></tr>");
            string s;
            sr = File.OpenText(testfile);
            s = sr.ReadLine();
            while (s != null)
            {
                if (i % 2 == 0)
                    sw.WriteLine("<tr>");
                sw.WriteLine("<td>" + s + "</td>");
                if ((i + 1) % 2 == 0)
                    sw.WriteLine("</tr>");
                s = sr.ReadLine();
                i++;
            }
            sr.Close();
            sw.WriteLine("</table></body></html>");
            sw.Close();
            web.Visible = true;
            web.Navigate("file://c:\\temp\\list.html");
        }

        private void runTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string s,output;
            int j, errors = 0;
            toolStripProgressBar1.Maximum = i / 2;
            StreamWriter sw, sw2;
            StreamReader sr = File.OpenText(testfile);
            sw2 = File.CreateText("c:\\temp\\report.html");
            sw2.WriteLine("<html><body><table width=\"100%\" style=\"border:1px solid;\"><tr><td style=\"background-color: #CCCCCC\" width=\"20%\">Input</td><td style=\"background-color: #CCCCCC\" width=\"40%\">Expected Output</td><td style=\"background-color: #CCCCCC\" width=\"40%\">Actual Output</td></tr>");
            for (j = 0; j < i / 2; j++ )
            {
                output = "No return value";
                s = sr.ReadLine();
                sw2.WriteLine("<tr>");
                sw2.WriteLine("<td>" + s + "</td>");
                sw = File.CreateText("c:\\temp\\test.bat");
                System.IO.File.Copy(rubyfile, "c:\\temp\\test.rb", true);
                sw.WriteLine("@ECHO OFF");
                sw.WriteLine("c:\\temp\\test.rb %1 %2 %3");
                sw.Close();
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\\temp\\test.bat");
                psi.RedirectStandardOutput = true;
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.CreateNoWindow = true;
                psi.UseShellExecute = false;
                psi.Arguments = s;
                System.Diagnostics.Process listFiles;
                listFiles = System.Diagnostics.Process.Start(psi);
                System.IO.StreamReader myOutput = listFiles.StandardOutput;
                listFiles.WaitForExit();
                if (listFiles.HasExited)
                {
                    output = myOutput.ReadToEnd();
                }
                s = sr.ReadLine();
                sw2.WriteLine("<td>" + s + "</td>");
                if (s != output)
                {
                    errors++;
                    sw2.WriteLine("<td style=\"background-color: #FF9999\">" + output + "</td>");
                }
                else
                {
                    sw2.WriteLine("<td>" + output + "</td>");
                }
                sw2.WriteLine("</tr>");
                toolStripProgressBar1.Value++;
            }
            MessageBox.Show(j + " tests in total, " + errors + " errors");
            sw2.WriteLine("</table>");
            sw2.WriteLine("<BR /> Number of tests: " + j + "<BR/> Tests passed: " + (j - errors) +"<BR/> Tests failed: " + errors + "</body></html>");
            sr.Close();
            sw2.Close();
            web.Navigate("file://c:\\temp\\report.html");
        }

        private void loadRubyProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open Ruby Program file";
            fDialog.Filter = "Ruby Program|*.rb";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                rubyfile = fDialog.FileName.ToString();
            }
            if (testfile != null && rubyfile != null)
                runTestToolStripMenuItem.Enabled = true;
        }
    }
}
